#include "plant.h"

int n_mainstream, n_window, n_room, n_area, n_instrument;
int **main_stream;
int *main_stream_colsize;

struct window **window_arr;
struct room **room_arr;
struct area **area_arr;
struct instrument **instrument_arr;

struct window* init_window(int i)
{
    struct window* tmp = (struct window*) malloc(sizeof(struct window));
    tmp->i = i;  // 窗口编号
    tmp->self_loop = (Window_information[i][0] == 1);
    int room_idx = Window_information[i][1];
    tmp->r = room_arr[room_idx];  // 窗口指向车间
    tmp->cost = Window_information[i][2];
    tmp->prework[0] = (Window_information[i][3] == 1);
    tmp->prework[1] = (Window_information[i][4] == 1);
    tmp->prework[2] = (Window_information[i][5] == 1);
    return tmp;
}

void construt_the_plant()
{
    // 计算数组长度
    n_mainstream = T;
    n_room = N;
    n_area = R;
    n_instrument = D;
    n_window = W+Loop_n*L;  // 窗口包含环回的部分
    for (int i = Loop_n; i < W; ++i) {
        if (Window_information[i][0] == 1) {
            n_window += L;
        }
    }

    // 初始化车间
    room_arr = (struct room**) malloc (n_room*sizeof(struct room*));
    for (int i = 0; i < n_room; ++i) {
        struct room* tmp = (struct room*) malloc(sizeof(struct room));
        tmp->i = i;
        tmp->ka = 0;
        tmp->a = (struct area**)malloc(n_area*sizeof(struct area*));
        room_arr[i] = tmp;
    }

    // 初始化窗口
    window_arr = (struct window**) malloc (n_window*sizeof(struct window*));
    int idx = 0;
    for (int i = 0; i <= L; ++i) {
        for (int j = 0; j < Loop_n; ++j) {
            struct window* tmp = init_window(j);
            window_arr[idx++] = tmp;
        }
    }
    for (int i = Loop_n; i < W; ++i) {
        if (Window_information[i][0] == 1) {
            for (int j = L; j >= 0; --j) {
                struct window* tmp = init_window(i);
                window_arr[idx++] = tmp;
            }
        } else {
            struct window* tmp = init_window(i);
            window_arr[idx++] = tmp;
        }
    }

    // 初始化车间区域
    area_arr = (struct area**) malloc (n_area*sizeof(struct area*));
    for (int i = 0; i < n_area; ++i) {
        int room_idx = Area_information[i][0];

        struct area* tmp = (struct area*) malloc(sizeof(struct area));
        tmp->i = i;
        tmp->r = room_arr[room_idx];
        tmp->energy_type = Area_information[i][1];
        area_arr[i] = tmp;

        room_arr[room_idx]->a[room_arr[room_idx]->ka] = tmp;  // 车间指向车间区域
        room_arr[room_idx]->ka += 1;
    }

    // 初始化仪器
    instrument_arr = (struct instrument**) malloc (D*sizeof(struct instrument*));
    for (int i = 0; i < D; ++i) {
        struct instrument* tmp = (struct instrument*) malloc(sizeof(struct instrument));
        tmp->i = i;
        tmp->t = Instrument_information[i][0];
        tmp->cost[0] = Instrument_information[i][1];
        tmp->cost[1] = Instrument_information[i][2];
        tmp->cost[2] = Instrument_information[i][3];
        tmp->cost[3] = Instrument_information[i][4];
        tmp->cost[4] = Instrument_information[i][5];
        tmp->main = false;
        tmp->fixed = false;
        instrument_arr[i] = tmp;
    }

    // 核心流水线
    main_stream = (int**)malloc(T*sizeof(int*));
    main_stream_colsize = (int*)malloc(T*sizeof(int));
    for (int i = 0; i < T; ++i) {
        main_stream_colsize[i] = F[i]+1;
        main_stream[i] = (int*)malloc((F[i]+1)*sizeof(int));
        for (int j = 0; j < F[i]; ++j) {
            main_stream[i][j] = Graph[Edge_idx[i][j]][1];
            instrument_arr[main_stream[i][j]]->main = true;
        }
        main_stream[i][F[i]] = Graph[Edge_idx[i][F[i]-1]][2];
        instrument_arr[main_stream[i][F[i]]]->main = true;
    }

//    printf("test~~~~~~~~~~~~~~~~~~~~~\n");
//    for (int i = 0; i < T; ++i) {
//        for (int j = 0; j < main_stream_colsize[i]; ++j) {
//            printf("%d ", main_stream[i][j]);
//        }
//        printf("\n");
//    }
//    printf("test~~~~~~~~~~~~~~~~~~~~~\n");
}
